package a03_哈希;

import java.util.*;

/**
 * <p>
 * a04_两数之和
 * </p>
 *
 * @author flyduck
 * @since 2024-06-24
 */
public class a05_两数之和 {

    public static void main(String[] args) {
        int[] nums = {2,7,11,15};
        int[] ints = twoSum(nums, 9);
        System.out.println(Arrays.toString(ints));
    }

    public static int[] twoSum(int[] nums, int target) {
        int[] result = new int[2];

        Map<Integer,Integer> map = new HashMap<>();

        for(int i = 0;i < nums.length;i++){
            if(map.containsKey(target - nums[i])){
                result[0] = map.get(target - nums[i]);
                result[1] = i;
            }else {
                map.put(nums[i],i);
            }
        }
        return result;
    }
}
